#pragma once

#include <iostream>
#include <string>
#include "TcpServer.hpp"
#include "HttpProtocol.hpp"

using namespace TcpServerModule;
const std::string Sep = "\r\n";
const std::string BlankLine = Sep;

class HttpServer
{
public:
    HttpServer(int port) : _tsvr(std::make_unique<TcpServer>(port))
    {
    }
    void Start()
    {
        _tsvr->InitServer([this](SockPtr sockfd, InetAddr client)
                          { return this->HanderHttpRequest(sockfd, client); });
        _tsvr->Loop();
    }
    // 处理http的入口
    bool HanderHttpRequest(SockPtr sockfd, InetAddr client)
    {
        LOG(LogLevel::DEBUG) << "get a new client:" << sockfd->Fd() << " addr info:" << client.Addr();
        std::string http_request;
        sockfd->Recv(&http_request);
        HttpRequest req;

        req.Deserialize(http_request);
        //std::cout << http_request.c_str() << std::endl;
        // 读取请求，对请求进行分析处理 --》文本处理
        // 1 读取请求完整性
        // 2完整http反序列化， http response序列化
        // demo1：返回一个固定内容
        // std::string status_line = "http/1.1 200 OK" + Sep + BlankLine;
        // std::string body = "<!DOCTYPE html>\
        //                    < html >\
        //                    <>\
        //                        Hello,World !\
        //                        </ body> < / html > ";
        // std::string httppresponse = status_line + body;
        // sockfd->Send(httppresponse);
        return true;
    }
    ~HttpServer()
    {
    }

private:
    std::unique_ptr<TcpServer> _tsvr;
};
